#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/statvfs.h>


int g_sd_free_space = 0;

static int inline is_sd_card_full()
{
    return g_sd_free_space < 60;	// 小于60M
}

int check_sd_card(double *value,double *total){
    int ret;
    struct statvfs stat;

    memset( &stat, 0, sizeof(stat));
    ret = statvfs( "/mnt/.", &stat );
    if( ret!=0 ){
        printf("Can't get fs detail from /mnt\n");
        printf("STATVFS ERR:%d   \n", ret);
        return 1;
    }

	*value = 0;
	*total = 0;
    
	printf("f_blocks = %ld,f_frsize = %ld,f_bfree = %ld\n",stat.f_blocks, stat.f_frsize, stat.f_bfree);
	printf("f_bavail = %ld, f_blocks = %ld\n", stat.f_bavail, stat.f_blocks);

    *value = (double)stat.f_bavail * stat.f_frsize /1024.0 /1024.0;
    *total = (double)stat.f_blocks * stat.f_frsize/1024.0/1024.0;

    printf("Free space of '/mnt' is %fM. total :%fM\n", *value,*total);

    g_sd_free_space = *value;
	printf("g_sd_free_space = %d\n", g_sd_free_space);

    if( is_sd_card_full() ){
        printf("SD IS FULL.    \n");
        return 1;
    }

    return 0;
}

void main()
{
	double value, total;

	check_sd_card(&value, &total);

	/* card is full */
    if (is_sd_card_full()) {
        printf("full\n");
    }

}

